package com.service.impl;

import com.mapper.DestroyMapper;
import com.mapper.DrugMapper;
import com.mapper.InventoryMapper;
import com.pojo.Admin;
import com.pojo.Drug;
import com.pojo.Inventory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Drug)表服务实现类
 *
 * @author makejava
 * @version 1.0
 * @since 2022-04-08 12:33:27
 */
@Service("drugService")
public class DrugServiceImpl {
    @Resource
    private DrugMapper drugMapper;
    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private DestroyMapper destroyMapper;

    //返回管理员首页相关数据
    public Map<String, Object> AdminDate(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("user");
        String no = admin.getNo();
        System.out.println("====================================================================================================================");
        System.out.println(no);
        HashMap<String, Object> map = new HashMap<>();

        int lower = inventoryMapper.countLowInventoryDrugs().size();//统计库存紧张药品种数
        int day = inventoryMapper.countClose2ExpiryPDbatches(); // 统计临期库存药品批数
        int variety = inventoryMapper.countInventoryDrugs();// 统计库存药品种数
        int total = inventoryMapper.countInventoryPDbatches();// 统计库存药品批数
        int destroy = destroyMapper.destroyAll();//销毁药品总数
        int adminCount = inventoryMapper.adminCount(no);//管理员入库库存总批数
        int adminDestroy = destroyMapper.adminDestroy(no);//管理员销毁药品总批数
        int adminCountAll = adminCount + adminDestroy;//管理员入库总批数
        int countAll = total + destroy;//所有入库总批次

        int comeProportion = (int) (((double) adminCountAll / (double) countAll) * 100);//管理员入库占比
        int stockProportion = (int) (((double) adminCount / (double) total) * 100);//入库的药品批数占总库存批数比例
        int desProportion = (int) (((double) adminDestroy / (double) destroy) * 100);//管理员销毁药品占比
        System.out.println(comeProportion);
        System.out.println(stockProportion);
        System.out.println(desProportion);
        System.out.println(lower);

        map.put("name", admin.getName());
        map.put("adminCountAll", adminCountAll);
        map.put("adminCount", adminCount);
        map.put("lower", lower);
        map.put("day", day);
        map.put("variety", variety);
        map.put("total", total);
        map.put("destroy", destroy);
        map.put("adminDestroy", adminDestroy);
        map.put("desProportion", desProportion);
        map.put("comeProportion", comeProportion);
        map.put("stockProportion", stockProportion);

        return map;
    }


    //查看库存紧张药品列表信息
    public List<Drug> queryLowInventoryDrugs() {
        return drugMapper.queryLowInventoryDrugs();
    }

    //查看库存临期药品列表信息
    public List<Inventory> queryClose2ExpiryPDbatches() {
        return drugMapper.queryClose2ExpiryPDbatches();
    }

    //查询库存药品列表
    public List<Inventory> allDrug() {
        return this.drugMapper.allDrug();
    }

    //查看库存药品批次明细
    public List<Drug> queryDrugOne() {
        return this.drugMapper.drugOne();
    }


    /**
     * 根据模糊条件查询总个数
     */
    public Map<String, Object> selectForCount(String name) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.drugMapper.selectForCount(name));
        return map;
    }

    /**
     * 查询所有数据
     *
     * @return 返回所有数据
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.drugMapper.selectAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param no 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(String no) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.drugMapper.selectById(no));
        return map;
    }


    /**
     * 新增数据
     *
     * @param drug 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(Drug drug) {
        // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
        this.drugMapper.insert(drug);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param drug 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(Drug drug) {
        this.drugMapper.updateById(drug);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param no 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(String no) {
        this.drugMapper.deleteById(no);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "删除成功");
        return map;
    }
}